#include <stdio.h>
#include <string.h>

void readInfo(char name[][30], float score[], int age[], int m);
void printStudentsArray(char name[][30], float score[], int age[], int m);
void sortByScore(char name[][30], float score[], int age[], int m);
void searchStudent(char name[][30], float score[], int age[], int m);
int main(int argc, char const *argv[])
{    
    char name[30][30];
    float score[30];
    int age[30];
    int m;
    printf("Input m:");
    scanf("%d", &m);
    readInfo(name, score, age, m);
    printStudentsArray(name, score, age, m);
    searchStudent(name, score, age, m);
    sortByScore(name, score, age, m);
    printStudentsArray(name, score, age, m);
    return 0;
}    

void readInfo(char name[][30], float score[], int age[], int m)
{    
    int i;
    for ( i = 0; i < m; ++i)
    {    
        printf("请输入新增学生的名字\n");
        scanf("%s", name[i]);
        printf("请输入新增学生的成绩\n");
        scanf("%f", &score[i]);
        printf("请输入新增学生的年龄\n");
        scanf("%d", &age[i]);
    }
}    

void printStudentsArray(char name[][30], float score[], int age[], int m)
{    
    printf("学生系统中一共有%d位学生信息\n", m);
    int i;
    for (i = 0; i < m; i++)
    {    
        printf("姓名:%s,成绩:%f,年龄:%d\n", name[i], score[i], age[i]);
    }
}    

void sortByScore(char name[][30], float score[], int age[], int m)
{    

    int i, j;
    for (  i = 0; i < m - 1; i++)
    {    
        for (  j = 0; j < m - 1 - i; j++)
        {    
            if ( score[j] > score[j + 1])
            {    
                float  tmpScore = score[j];
                int   tmpAge = age[j];
                char tmpName[30] = {};
                strcpy(tmpName, name[j]);
                score[j] = score[j + 1];
                age[j] = age[j + 1];
                strcpy(name[j], name[j + 1]);
                score[j + 1] = tmpScore;
                age[j + 1] = tmpAge;
                strcpy(name[j + 1], tmpName);

            }
        }
    }
}    

//根据名字查询学生
void searchStudent(char name[][30], float score[], int age[], int m)
{    
    char sname[30] = {};
    printf("请输入要查询的学生姓名\n");
    scanf("%s", sname);
    int i, j;
    int index = -1;
    for (i = 0; i < m; i++)
    {    
        if (strcmp(name[i], sname) == 0)
        {    
            index = i;
        }
    }
    if (index == -1)
    {    
        printf("没有找到该学生信息\n");
    }
    else
    {    
        printf("您要查询的学生信息是 姓名:%s,成绩:%f,年龄:%d\n", name[index], score[index], age[index]);
    }
    return;
}